package cn.qzuie.smart.module.dorm.service.billpay;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.qzuie.smart.module.dorm.controller.admin.billpay.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.billpay.BillPayDO;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;

import cn.qzuie.smart.module.dorm.dal.mysql.billpay.BillPayMapper;

import static cn.qzuie.smart.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.convertList;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.diffList;
import static cn.qzuie.smart.module.dorm.enums.ErrorCodeConstants.*;

/**
 * 账单支付 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BillPayServiceImpl implements BillPayService {

    @Resource
    private BillPayMapper billPayMapper;

    @Override
    public Long createBillPay(BillPaySaveReqVO createReqVO) {
        // 插入
        BillPayDO billPay = BeanUtils.toBean(createReqVO, BillPayDO.class);
        billPayMapper.insert(billPay);

        // 返回
        return billPay.getId();
    }

    @Override
    public void updateBillPay(BillPaySaveReqVO updateReqVO) {
        // 校验存在
        validateBillPayExists(updateReqVO.getId());
        // 更新
        BillPayDO updateObj = BeanUtils.toBean(updateReqVO, BillPayDO.class);
        billPayMapper.updateById(updateObj);
    }

    @Override
    public void deleteBillPay(Long id) {
        // 校验存在
        validateBillPayExists(id);
        // 删除
        billPayMapper.deleteById(id);
    }

    @Override
        public void deleteBillPayListByIds(List<Long> ids) {
        // 删除
        billPayMapper.deleteByIds(ids);
        }


    private void validateBillPayExists(Long id) {
        if (billPayMapper.selectById(id) == null) {
            throw exception(BILL_PAY_NOT_EXISTS);
        }
    }

    @Override
    public BillPayDO getBillPay(Long id) {
        return billPayMapper.selectById(id);
    }

    @Override
    public PageResult<BillPayDO> getBillPayPage(BillPayPageReqVO pageReqVO) {
        return billPayMapper.selectPage(pageReqVO);
    }

}